package ag.data.Regulation.group;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

public class GroupData {
    int size;
    String[] arr;

    public GroupData(int size) {
        this.size = size;
        this.arr = new String[1];
    }


    public static List<GroupData> insertData(List<String> exceptEqual, int size) {
        List<GroupData> groupDataList = new ArrayList<>();
        for (int i = 0; i < exceptEqual.size(); i++) {
            String data = exceptEqual.get(i);
            if (i == 0) {
                GroupData groupData = new GroupData(size);
                groupData.insertData(data);
                groupDataList.add(groupData);
            } else {
                GroupData groupData = groupDataList.get(groupDataList.size() - 1);
                if (groupData.arr.length == groupData.size) {
                    GroupData groupDataNew = new GroupData(size);
                    groupDataNew.insertData(data);
                    groupDataList.add(groupDataNew);
                } else {
                    groupData.insertData(data);
                }
            }
        }
        return groupDataList;
    }

    public static void writeList(List<GroupData> groupDataList) {
        for (GroupData groupData : groupDataList) {
            for (int i = 0; i < groupData.arr.length; i++) {
                System.out.print(groupData.arr[i]);
            }
            System.out.print(",");
        }
        System.out.println();
    }

    public static String getListHtml(List<GroupData> groupDataList, int size) {
        StringBuffer stringBuffer = new StringBuffer();
        for (int j = 0; j < size; j++) {
            StringBuffer stringBufferLine = new StringBuffer();
            StringBuffer stringBufferLineTip = new StringBuffer("Line" + j + ":");
            String tmp = "";
            for (int i = 0; i < groupDataList.size(); i++) {
                GroupData groupData = groupDataList.get(i);
                stringBufferLine.append(groupData.getIndexHtml(j));
            }
            stringBufferLine.append("<br/>");
            stringBuffer.append(stringBufferLine);
        }
        stringBuffer.append("<br/>");
        return stringBuffer.toString();
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public String[] getArr() {
        return arr;
    }

    public void setArr(String[] arr) {
        this.arr = arr;
    }

    public String getGroupDataByIndex(int index) {
        if (index > size) {
            return null;
        }
        return arr[index];
    }

    public String getIndexHtml(int index) {
        StringBuffer stringBuffer = new StringBuffer();
        if (index >= arr.length) {
            return stringBuffer.toString();
        }
        if (arr[index].equals("庄")) {
            stringBuffer.append("<span style='color:red'>" + arr[index] + "</span>");
        } else {
            stringBuffer.append(arr[index]);
        }
        return stringBuffer.toString();
    }

    public GroupData getOpsite() {
        GroupData opsiteGroupData = new GroupData(this.size);

        for (int i = 0; i < arr.length; i++) {
            String result = arr[i];
            if ("庄".equals(result)) {
                result = "闲";
            }
            if ("闲".equals(result)) {
                result = "庄";
            }
            opsiteGroupData.insertData(result);
        }
        return opsiteGroupData;
    }

    public GroupData insertData(String data) {
        int nowLength = arr.length;
        if (nowLength == size) {
            GroupData newGroupData = new GroupData(this.size);
            newGroupData = newGroupData.insertData(data);
            return newGroupData;
        }
        if (null == arr[0]) {
            arr[0] = data;
            return this;
        }
        arr = Arrays.copyOf(arr, arr.length + 1);
        arr[arr.length - 1] = data;
        return this;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        GroupData groupData = (GroupData) o;
        return size == groupData.size &&
                Arrays.equals(arr, groupData.arr);
    }

    @Override
    public int hashCode() {
        int result = Objects.hash(size);
        result = 31 * result + Arrays.hashCode(arr);
        return result;
    }
}
